<!--
  商户审核对话框组件
  @description 商户审核功能的对话框组件
  @author AI Assistant
  @since 1.0.0
-->
<template>
  <Dialog v-model="dialogVisible" :title="t('settle.merchant.audit')" width="600px">
    <div v-loading="loading">
      <!-- 商户信息展示 -->
      <el-descriptions :column="2" border class="mb-20px">
        <el-descriptions-item :label="t('settle.merchant.merchantNo')">
          {{ merchantData?.merchantNo }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.merchant.merchantName')">
          {{ merchantData?.merchantName }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.merchant.merchantType')">
          <el-tag :type="getMerchantTypeTagType(merchantData?.merchantType)">
            {{ merchantTypeOptions[merchantData?.merchantType || 0] }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.merchant.channelType')">
          <el-tag :type="getChannelTypeTagType(merchantData?.channelType)">
            {{ channelTypeOptions[merchantData?.channelType || 0] }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.merchant.contactName')">
          {{ merchantData?.contactName }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.merchant.contactPhone')">
          {{ merchantData?.contactPhone }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.merchant.contactEmail')">
          {{ merchantData?.contactEmail || '-' }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.merchant.settleCycle')">
          {{ settleCycleOptions[merchantData?.settleCycle || 0] }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.merchant.settleAccount')" :span="2">
          {{ merchantData?.settleAccount }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.common.createTime')" :span="2">
          {{ formatDate(merchantData?.createTime) }}
        </el-descriptions-item>
      </el-descriptions>

      <!-- 审核表单 -->
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item :label="t('settle.merchant.auditResult')" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="MerchantStatusEnum.APPROVED">
              <el-tag type="success">{{ t('settle.merchant.auditPass') }}</el-tag>
            </el-radio>
            <el-radio :label="MerchantStatusEnum.REJECTED">
              <el-tag type="danger">{{ t('settle.merchant.auditReject') }}</el-tag>
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item :label="t('settle.merchant.auditRemark')" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            :placeholder="t('settle.common.pleaseInput') + t('settle.merchant.auditRemark')"
            :rows="4"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">
        {{ t('settle.common.confirm') }}
      </el-button>
      <el-button @click="dialogVisible = false">
        {{ t('settle.common.cancel') }}
      </el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { formatDate } from '@/utils/formatTime'
import { auditMerchantApi } from '@/api/settle/merchant'
import type { MerchantVO } from '@/api/settle/types'
import {
  MerchantStatusEnum,
  MerchantTypeEnum,
  ChannelTypeEnum,
  SettleCycleEnum
} from '@/api/settle/types'
import { Dialog } from '@/components/Dialog'

defineOptions({ name: 'MerchantAuditDialog' })

const { t } = useI18n()

// 弹窗相关
const dialogVisible = ref(false)
const loading = ref(false)

// 商户数据
const merchantData = ref<MerchantVO>()

// 表单相关
const formRef = ref<FormInstance>()
const formData = ref({
  id: 0,
  status: MerchantStatusEnum.APPROVED,
  remark: ''
})

// 选项数据
const merchantTypeOptions = {
  [MerchantTypeEnum.ENTERPRISE]: t('settle.merchant.merchantTypes.enterprise'),
  [MerchantTypeEnum.INDIVIDUAL]: t('settle.merchant.merchantTypes.individual'),
  [MerchantTypeEnum.MICRO]: t('settle.merchant.merchantTypes.micro')
}

const channelTypeOptions = {
  [ChannelTypeEnum.HUIFU]: t('settle.merchant.channelTypes.huifu'),
  [ChannelTypeEnum.UNIONPAY]: t('settle.merchant.channelTypes.unionpay')
}

const settleCycleOptions = {
  [SettleCycleEnum.DAILY]: t('settle.merchant.settleCycles.daily'),
  [SettleCycleEnum.WEEKLY]: t('settle.merchant.settleCycles.weekly'),
  [SettleCycleEnum.MONTHLY]: t('settle.merchant.settleCycles.monthly')
}

// 表单校验规则
const formRules = reactive<FormRules>({
  status: [
    {
      required: true,
      message: t('settle.common.pleaseSelect') + t('settle.merchant.auditResult'),
      trigger: 'change'
    }
  ],
  remark: [
    {
      required: true,
      message: t('settle.common.pleaseInput') + t('settle.merchant.auditRemark'),
      trigger: 'blur'
    }
  ]
})

// 事件定义
const emit = defineEmits(['success'])

/**
 * 打开对话框
 */
const open = (merchant: MerchantVO) => {
  dialogVisible.value = true
  merchantData.value = merchant
  resetForm()
  formData.value.id = merchant.id
}

/**
 * 提交审核
 */
const submitForm = async () => {
  const valid = await formRef.value?.validate()
  if (!valid) return

  loading.value = true
  try {
    await auditMerchantApi(formData.value.id, formData.value.status, formData.value.remark)
    ElMessage.success(t('settle.merchant.tips.auditSuccess'))
    dialogVisible.value = false
    emit('success')
  } catch (error) {
    console.error('审核商户失败:', error)
  } finally {
    loading.value = false
  }
}

/**
 * 重置表单
 */
const resetForm = () => {
  formData.value = {
    id: 0,
    status: MerchantStatusEnum.APPROVED,
    remark: ''
  }
  formRef.value?.resetFields()
}

// 标签类型获取函数
const getMerchantTypeTagType = (type?: number) => {
  if (!type) return ''
  const typeMap = {
    [MerchantTypeEnum.ENTERPRISE]: 'primary',
    [MerchantTypeEnum.INDIVIDUAL]: 'success',
    [MerchantTypeEnum.MICRO]: 'warning'
  }
  return typeMap[type] || ''
}

const getChannelTypeTagType = (type?: number) => {
  if (!type) return ''
  const typeMap = {
    [ChannelTypeEnum.HUIFU]: 'primary',
    [ChannelTypeEnum.UNIONPAY]: 'success'
  }
  return typeMap[type] || ''
}

// 暴露方法
defineExpose({ open })
</script>

<style scoped>
.mb-20px {
  margin-bottom: 20px;
}
</style>
